home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / sun / awt / AppContext.class (.txt) next >
Encoding:
Java Class File  |  2006-11-29  |  6.7 KB  |  297 lines

  1. package sun.awt;
  2.  
  3. import java.awt.Toolkit;
  4. import java.awt.event.InvocationEvent;
  5. import java.beans.PropertyChangeListener;
  6. import java.beans.PropertyChangeSupport;
  7. import java.security.AccessController;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Hashtable;
  11. import java.util.Set;
  12.  
  13. public final class AppContext {
  14.    public static final Object EVENT_QUEUE_KEY = new StringBuffer("EventQueue");
  15.    private static Hashtable threadGroup2appContext = null;
  16.    private static AppContext mainAppContext = null;
  17.    private final HashMap table = new HashMap();
  18.    private final ThreadGroup threadGroup;
  19.    private PropertyChangeSupport changeSupport = null;
  20.    public static final String DISPOSED_PROPERTY_NAME = "disposed";
  21.    public static final String GUI_DISPOSED = "guidisposed";
  22.    private boolean isDisposed = false;
  23.    private static int numAppContexts;
  24.    private final ClassLoader contextClassLoader;
  25.    private static MostRecentThreadAppContext mostRecentThreadAppContext;
  26.    private long DISPOSAL_TIMEOUT = 5000L;
  27.    private long THREAD_INTERRUPT_TIMEOUT = 1000L;
  28.    private MostRecentKeyValue mostRecentKeyValue = null;
  29.    private MostRecentKeyValue shadowMostRecentKeyValue = null;
  30.  
  31.    public static Set getAppContexts() {
  32.       return threadGroup2appContext == null ? new HashSet() : new HashSet(((Hashtable)threadGroup2appContext.clone()).values());
  33.    }
  34.  
  35.    public boolean isDisposed() {
  36.       return this.isDisposed;
  37.    }
  38.  
  39.    AppContext(ThreadGroup var1) {
  40.       ++numAppContexts;
  41.       if (threadGroup2appContext == null) {
  42.          threadGroup2appContext = new Hashtable(2, 0.2F);
  43.       }
  44.  
  45.       this.threadGroup = var1;
  46.       threadGroup2appContext.put(var1, this);
  47.       this.contextClassLoader = (ClassLoader)AccessController.doPrivileged(new 2(this));
  48.    }
  49.  
  50.    public static final AppContext getAppContext() {
  51.       if (numAppContexts == 1) {
  52.          return mainAppContext;
  53.       } else {
  54.          Thread var0 = Thread.currentThread();
  55.          AppContext var1 = null;
  56.          MostRecentThreadAppContext var2 = mostRecentThreadAppContext;
  57.          if (var2 != null && var2.thread == var0) {
  58.             var1 = var2.appContext;
  59.          } else {
  60.             var1 = (AppContext)AccessController.doPrivileged(new 3(var0));
  61.          }
  62.  
  63.          if (var1 == mainAppContext) {
  64.             SecurityManager var3 = System.getSecurityManager();
  65.             if (var3 != null && var3 instanceof AWTSecurityManager) {
  66.                AWTSecurityManager var4 = (AWTSecurityManager)var3;
  67.                AppContext var5 = var4.getAppContext();
  68.                if (var5 != null) {
  69.                   var1 = var5;
  70.                }
  71.             }
  72.          }
  73.  
  74.          return var1;
  75.       }
  76.    }
  77.  
  78.    public void dispose() throws IllegalThreadStateException {
  79.       if (this.threadGroup.parentOf(Thread.currentThread().getThreadGroup())) {
  80.          throw new IllegalThreadStateException("Current Thread is contained within AppContext to be disposed.");
  81.       } else {
  82.          synchronized(this) {
  83.             if (this.isDisposed) {
  84.                return;
  85.             }
  86.  
  87.             this.isDisposed = true;
  88.          }
  89.  
  90.          PropertyChangeSupport var1 = this.changeSupport;
  91.          if (var1 != null) {
  92.             var1.firePropertyChange("disposed", false, true);
  93.          }
  94.  
  95.          Object var2 = new Object();
  96.          4 var3 = new 4(this, var1, var2);
  97.          synchronized(var2) {
  98.             SunToolkit.postEvent(this, new InvocationEvent(Toolkit.getDefaultToolkit(), var3));
  99.  
  100.             try {
  101.                var2.wait(this.DISPOSAL_TIMEOUT);
  102.             } catch (InterruptedException var18) {
  103.             }
  104.          }
  105.  
  106.          5 var21 = new 5(this, var2);
  107.          synchronized(var2) {
  108.             SunToolkit.postEvent(this, new InvocationEvent(Toolkit.getDefaultToolkit(), var21));
  109.  
  110.             try {
  111.                var2.wait(this.DISPOSAL_TIMEOUT);
  112.             } catch (InterruptedException var16) {
  113.             }
  114.          }
  115.  
  116.          this.threadGroup.interrupt();
  117.          long var4 = System.currentTimeMillis();
  118.          long var6 = var4 + this.THREAD_INTERRUPT_TIMEOUT;
  119.  
  120.          while(this.threadGroup.activeCount() > 0 && System.currentTimeMillis() < var6) {
  121.             try {
  122.                Thread.sleep(10L);
  123.             } catch (InterruptedException var15) {
  124.             }
  125.          }
  126.  
  127.          this.threadGroup.stop();
  128.          var4 = System.currentTimeMillis();
  129.          var6 = var4 + this.THREAD_INTERRUPT_TIMEOUT;
  130.  
  131.          while(this.threadGroup.activeCount() > 0 && System.currentTimeMillis() < var6) {
  132.             try {
  133.                Thread.sleep(10L);
  134.             } catch (InterruptedException var14) {
  135.             }
  136.          }
  137.  
  138.          int var8 = this.threadGroup.activeGroupCount();
  139.          if (var8 > 0) {
  140.             ThreadGroup[] var9 = new ThreadGroup[var8];
  141.             var8 = this.threadGroup.enumerate(var9);
  142.  
  143.             for(int var10 = 0; var10 < var8; ++var10) {
  144.                threadGroup2appContext.remove(var9[var10]);
  145.             }
  146.          }
  147.  
  148.          threadGroup2appContext.remove(this.threadGroup);
  149.          MostRecentThreadAppContext var25 = mostRecentThreadAppContext;
  150.          if (var25 != null && var25.appContext == this) {
  151.             mostRecentThreadAppContext = null;
  152.          }
  153.  
  154.          try {
  155.             this.threadGroup.destroy();
  156.          } catch (IllegalThreadStateException var13) {
  157.          }
  158.  
  159.          synchronized(this.table) {
  160.             this.table.clear();
  161.          }
  162.  
  163.          --numAppContexts;
  164.          this.mostRecentKeyValue = null;
  165.       }
  166.    }
  167.  
  168.    static void stopEventDispatchThreads() {
  169.       for(AppContext var1 : getAppContexts()) {
  170.          if (!var1.isDisposed()) {
  171.             PostShutdownEventRunnable var2 = new PostShutdownEventRunnable(var1);
  172.             if (var1 != getAppContext()) {
  173.                CreateThreadAction var3 = new CreateThreadAction(var1, var2);
  174.                Thread var4 = (Thread)AccessController.doPrivileged(var3);
  175.                var4.start();
  176.             } else {
  177.                var2.run();
  178.             }
  179.          }
  180.       }
  181.  
  182.    }
  183.  
  184.    public Object get(Object var1) {
  185.       synchronized(this.table) {
  186.          MostRecentKeyValue var3 = this.mostRecentKeyValue;
  187.          if (var3 != null && var3.key == var1) {
  188.             return var3.value;
  189.          } else {
  190.             Object var4 = this.table.get(var1);
  191.             if (this.mostRecentKeyValue == null) {
  192.                this.mostRecentKeyValue = new MostRecentKeyValue(var1, var4);
  193.                this.shadowMostRecentKeyValue = new MostRecentKeyValue(var1, var4);
  194.             } else {
  195.                MostRecentKeyValue var5 = this.mostRecentKeyValue;
  196.                this.shadowMostRecentKeyValue.setPair(var1, var4);
  197.                this.mostRecentKeyValue = this.shadowMostRecentKeyValue;
  198.                this.shadowMostRecentKeyValue = var5;
  199.             }
  200.  
  201.             return var4;
  202.          }
  203.       }
  204.    }
  205.  
  206.    public Object put(Object var1, Object var2) {
  207.       synchronized(this.table) {
  208.          MostRecentKeyValue var4 = this.mostRecentKeyValue;
  209.          if (var4 != null && var4.key == var1) {
  210.             var4.value = var2;
  211.          }
  212.  
  213.          return this.table.put(var1, var2);
  214.       }
  215.    }
  216.  
  217.    public Object remove(Object var1) {
  218.       synchronized(this.table) {
  219.          MostRecentKeyValue var3 = this.mostRecentKeyValue;
  220.          if (var3 != null && var3.key == var1) {
  221.             var3.value = null;
  222.          }
  223.  
  224.          return this.table.remove(var1);
  225.       }
  226.    }
  227.  
  228.    public ThreadGroup getThreadGroup() {
  229.       return this.threadGroup;
  230.    }
  231.  
  232.    public ClassLoader getContextClassLoader() {
  233.       return this.contextClassLoader;
  234.    }
  235.  
  236.    public String toString() {
  237.       return this.getClass().getName() + "[threadGroup=" + this.threadGroup.getName() + "]";
  238.    }
  239.  
  240.    public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
  241.       return this.changeSupport == null ? new PropertyChangeListener[0] : this.changeSupport.getPropertyChangeListeners();
  242.    }
  243.  
  244.    public synchronized void addPropertyChangeListener(String var1, PropertyChangeListener var2) {
  245.       if (var2 != null) {
  246.          if (this.changeSupport == null) {
  247.             this.changeSupport = new PropertyChangeSupport(this);
  248.          }
  249.  
  250.          this.changeSupport.addPropertyChangeListener(var1, var2);
  251.       }
  252.    }
  253.  
  254.    public synchronized void removePropertyChangeListener(String var1, PropertyChangeListener var2) {
  255.       if (var2 != null && this.changeSupport != null) {
  256.          this.changeSupport.removePropertyChangeListener(var1, var2);
  257.       }
  258.    }
  259.  
  260.    public synchronized PropertyChangeListener[] getPropertyChangeListeners(String var1) {
  261.       return this.changeSupport == null ? new PropertyChangeListener[0] : this.changeSupport.getPropertyChangeListeners(var1);
  262.    }
  263.  
  264.    // $FF: synthetic method
  265.    static AppContext access$002(AppContext var0) {
  266.       mainAppContext = var0;
  267.       return var0;
  268.    }
  269.  
  270.    // $FF: synthetic method
  271.    static int access$102(int var0) {
  272.       numAppContexts = var0;
  273.       return var0;
  274.    }
  275.  
  276.    // $FF: synthetic method
  277.    static AppContext access$000() {
  278.       return mainAppContext;
  279.    }
  280.  
  281.    // $FF: synthetic method
  282.    static Hashtable access$200() {
  283.       return threadGroup2appContext;
  284.    }
  285.  
  286.    // $FF: synthetic method
  287.    static MostRecentThreadAppContext access$302(MostRecentThreadAppContext var0) {
  288.       mostRecentThreadAppContext = var0;
  289.       return var0;
  290.    }
  291.  
  292.    static {
  293.       AccessController.doPrivileged(new 1());
  294.       mostRecentThreadAppContext = null;
  295.    }
  296. }
  297.